<?php

/**
 * $user 是User类实例
 */

class UserValidate{
    private $user;
    
    function __construct($user) {
        $this->user = $user;
    }
    
    /*
     * 验证用户名
     */
    public function validateUsername(){
        /*
        return preg_match('/^[A-Z0-9]{2,20}$/i', $this->user->getName());
         */
        return true;
    }
    
    /*
     * 验证密码
     */
    public function validatePassword(){
        return true;
    }
    
    public function validateDB($linkIdentify ){
        $queryStr = sprintf("select * from User where name='%s' and password='%s'",
                                        $this->user->name,
                                        $this->user->password
        );
        $query = mysql_query($queryStr, $linkIdentify);
        if(!$query){
            $message ='Invalid query: '.mysql_error()."\n";
            $message .= 'Whole query: '.$queryStr;
            die($message);
        }
        
        $result = mysql_fetch_array($query);
        if($result!=false){
            return true;
        }
        return false;
    }
    
    public function validate($linkIdentify){
        if (!$this->validateUsername()) {
            return false;
        }
        if (!$this->validatePassword()) {
            return false;
        }
        if (!$this->validateDB($linkIdentify)) {
            return false;
        }
        return true;
    }
    
}
